package com.atguigu.eduservice.controller;


import com.atguigu.annotation.WebLog;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 陈烨庆
 * @since 2022-01-24
 */
@RestController
@RequestMapping("/eduservice/edu-chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 课程大纲列表，根据课程id进行查询
     * @return
     */
    @WebLog("课程大纲列表")
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo", list);
    }

    @WebLog("添加章节")
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    @WebLog("查询章节")
    @GetMapping("getChapterInfo/{id}")
    public R getChapterInfo(@PathVariable String id) {
        EduChapter eduChapter = eduChapterService.getById(id);
        return R.ok().data("chapter", eduChapter);
    }

    @WebLog("更新章节")
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    @WebLog("删除章节")
    @DeleteMapping("removeChapter/{id}")
    public R removeChapter(@PathVariable String id) {
        if (eduChapterService.removeChapter(id)) {
            return R.ok();
        }
        return R.error();
    }

}

